/*------------------------------------------------------------------------
 *
 * geqo_random.c
 *	   随机数生成器
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * src/backend/optimizer/geqo/geqo_random.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include "optimizer/geqo_random.h"


void geqo_set_seed(PlannerInfo *fc_root, double fc_seed)
{
	GeqoPrivateData *fc_private = (GeqoPrivateData *) fc_root->join_search_private;

	pg_prng_fseed(&fc_private->random_state, fc_seed);
}

double geqo_rand(PlannerInfo *fc_root)
{
	GeqoPrivateData *fc_private = (GeqoPrivateData *) fc_root->join_search_private;

	return pg_prng_double(&fc_private->random_state);
}

int geqo_randint(PlannerInfo *fc_root, int fc_upper, int fc_lower)
{
	GeqoPrivateData *fc_private = (GeqoPrivateData *) fc_root->join_search_private;

	/*
	 * 在当前使用中，“lower”从不为负数，因此我们可以直接使用
	 * pg_prng_uint64_range。
	 */
	return (int) pg_prng_uint64_range(&fc_private->random_state, fc_lower, fc_upper);
}
